package model;

import java.awt.SystemColor;
import java.awt.Toolkit;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.UIManager;

import org.jdesktop.swingx.JXDatePicker;
import org.jdesktop.swingx.JXMonthView;
import org.jdesktop.swingx.plaf.basic.CalendarHeaderHandler;
import org.jdesktop.swingx.plaf.basic.SpinningCalendarHeaderHandler;

public class DatePicker {
	public static final String DEFAULT_ICON = "/images/calendar.png";
	public static final int DEFAULT_WIDTH_COLUMN = 18;

	public static JXDatePicker createDatePicker(int widthColumn, String path) {
		UIManager.put(CalendarHeaderHandler.uiControllerID,
				SpinningCalendarHeaderHandler.class.getName());
		JXDatePicker datePicker = new JXDatePicker();
		decorator(datePicker, widthColumn, path);
		return datePicker;
	}

	public static void decorator(JXDatePicker datePicker, int widthColumn,
			String path) {
		datePicker.setFormats(new String[] { "yyyy-MM-dd", "yyyy.MM.dd",
				"yyyy/MM/dd" });
		if (widthColumn > 0)
			datePicker.getEditor().setColumns(widthColumn);
		Date date = new Date(); // get today
		DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		String today = "Hôm nay: ".concat(dateFormat.format(date));
		datePicker.setLinkDay(date, today);
		datePicker.setDate(date);

		JXMonthView monthView = datePicker.getMonthView();
		monthView.setDaysOfTheWeek(new String[] { "CN", "Hai", "Ba", "Tư",
				"Năm", "Sáu", "Bảy" });
		monthView.setTodayBackground(SystemColor.textHighlight);
		monthView.setZoomable(true);
		monthView.setShowingTrailingDays(true);
		monthView.setShowingLeadingDays(true);
		monthView.setComponentInputMapEnabled(true);

		// set icon choose date
		try {
			URL url = Toolkit.getDefaultToolkit().getClass().getResource(path);
			ImageIcon icon = new ImageIcon(url);
			JButton btn = (JButton) datePicker.getComponent(1);
			btn.setIcon(icon);
		} catch (NullPointerException e) {
			// e.printStackTrace();
			// exception path is null
		}
	}
}
